const db = wx.cloud.database({
  env: 'cloud1-2g6vpgs796710242'
})
const app = getApp()
var id = ""

Page({
  data: {
    conference_info:[],
    show: false,
    duration: 300,
    position: 'right',
    round: true,
    overlay: true,
    customStyle: '',
    overlayStyle: '',
    aim:[],
    userInfo:[],
    npast:true
  },

  onLoad: function (options) {
    id = options.id
    db.collection('Conference')
    .doc(id)
    .get()
    .then(res=>{
      console.log(res)
      this.setData({
        conference_info:res.data
      })
    })
    .catch(err=>{
      console.log("conference云数据库",err)
    })

    db.collection("User")
    .where({
      _openid: app.globalData.openid
    })
    .get()
    .then(res=>{
      console.log("userInfo",res)
      if (!res.data.length==0) {
        this.setData({
          npast:false
        })
        console.log("不给予投递",this.data.npast)
      }
    })
    .catch(err=>{
      console.log(err)
    })
  },

  pop_cabinet(e) {
    console.log("拉起仓储",e.currentTarget.dataset.openid)
    var id = e.currentTarget.dataset.openid
    db.collection('User')
    .where({
      openid: id
    })
    .get()
    .then(res=>{
      console.log(res)
      this.setData({
        aim:res.data[0]
      })
    })
    .catch(err=>{
      console.log("用户获取失败",err)
    })
    const position = "bottom"
    let customStyle = ''
    let duration = this.data.duration
    switch (position) {
      case 'top':
      case 'bottom':
        customStyle = 'height: 20%;'
        break
      case 'right':
        break
    }
    this.setData({
      position,
      show: true,
      customStyle,
      duration
    })
  },

  join: function() {
    wx.showModal({
      title: "申请<SHARE>",
      cancelText:"再想想",
      confirmText:"Join！",
      editable:true,
      placeholderText:"贴上你对于项目的独有链接，builder审核后方可录入",
      success: (res)=> {
        if (res.confirm) {
          console.log('用户点击确定')
          if (this.data.npast==false && !intext=="") {
            console.log('用户点击确定',res.content)
            var intext = res.content
            db.collection("Conference")
            .doc(id)
            .update({
              data:{
                Share:_.push({
                  check: false,
                  openid: app.globalData.openid,
                  proposal: intext,
                  time: new Date().toJSON().substring(0, 10),
                  uhead: this.data.userInfo.avatarUrl,
                  uname: this.data.userInfo.nickName
                })
              }
            })
            .then(res=>{
              console.log("提交share",res)
            })
            .catch(err=>{
              console.log("提交share fail",err)
            })
          } else if (this.data.npast==false && intext=="") {
            wx.showToast({
              title: '填写信息不能为空',
              icon: "none"
            })
          } else if (this.data.npast==true) {
            wx.showToast({
              title: '请先于首页侧边栏授权登入个人信息',
              icon: "none"
            })
          } 
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  elink(e) {
    console.log(e.currentTarget.dataset)
    var linktype = e.currentTarget.dataset.type
    if (linktype=="Project") {
      wx.navigateTo({
        url: '../baike/baike?link=' + this.data.conference_info.Project,
      })
    }
    if (linktype=="Contest") {
      wx.navigateTo({
        url: '../baike/baike?link=' + this.data.conference_info.Contest,
      })
    } else {
      wx.navigateTo({
        url: '../baike/baike?link=' + this.data.conference_info.Other,
      })
    }
  },

  plink(e) {
    console.log(e.currentTarget.dataset.plink)
    var link = e.currentTarget.dataset.plink
    wx.navigateTo({
      url: '../baike/baike?link=' + link,
    })
  },

  onShareAppMessage: function () {

  }
})